﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Text;
using System.Xml;
using System.Configuration;

/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();

    public WebService () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public DataSet Data(string dwmch, string srq, string erq)
    {
        DataSet ds = new DataSet();
        if (string.IsNullOrEmpty(dwmch))
        {
            ds = mssql.ExecuteDs("select djbh,rq,dwbh,rtrim(beizhu) as beizhu,count(*) as tm from [spls_ck](nolock) where rq>='" + srq + "' and rq<='" + erq + "' group by djbh,rq,dwbh,beizhu order by rq,djbh");
        }
        else
        {
            ds = mssql.ExecuteDs("select djbh,rq,dwbh,rtrim(beizhu) as beizhu,count(*) as tm from [spls_ck](nolock) where dwbh in(select dwbh from [mchk](nolock) where dwmch='" + dwmch + "') and rq>='" + srq + "' and rq<='" + erq + "' group by djbh,rq,dwbh,beizhu order by rq,djbh");
        } 
        return ds;
    }

    public string mssqlConnect = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ToString();

    [WebMethod]
    public DataSet DataAll(string dwmch, string srq, string erq)
    {
        DataSet ds = new DataSet();
        try
        {            
            string sql = "";
            if (string.IsNullOrEmpty(dwmch))
            {
                sql = "select a.djbh,a.rq,a.dwbh,rtrim(a.beizhu) as beizhu,count(*) as tm "
                    +" from [pf_djhz] a(nolock), [pf_djmx] b(nolock) where a.djbh=b.djbh and a.rq>='" + srq + "' and a.rq<='" + erq + "' "
                    +" group by a.djbh,a.rq,a.dwbh,a.beizhu order by a.rq,a.djbh";
                Init.WriteLog(sql, true);
                ds = mssql.ExecuteDs(sql, mssqlConnect);
            }
            else
            {
                sql = "select a.djbh,a.rq,a.dwbh,rtrim(a.beizhu) as beizhu,count(*) as tm "
                    + " from [pf_djhz] a(nolock), [pf_djmx] b(nolock) where a.djbh=b.djbh and a.rq>='" + srq + "' and a.rq<='" + erq + "' "
                    + " group by a.djbh,a.rq,a.dwbh,a.beizhu order by a.rq,a.djbh";
                Init.WriteLog(sql, true);
                ds = mssql.ExecuteDs(sql, mssqlConnect);
            }
        }
        catch (Exception ex)
        {
            Init.WriteLog(ex.ToString(), true);
        }
        return ds;
    }

    [WebMethod]
    public bool CheckLogin(string userName,string userPass)
    {
        bool flag = false;

        return flag;
    }


    [WebMethod]
    public XmlDataDocument GetUpMassageDate(string dwmch, string srq, string erq)
    {
        try
        {
            XmlDataDocument xd = new XmlDataDocument();
            DataSet ds = new DataSet();
            if (string.IsNullOrEmpty(dwmch)) 
            {
                ds = mssql.ExecuteDs("select djbh,rq,dwbh,rtrim(beizhu) as beizhu,count(*) as tm from [spls_ck](nolock) where rq>='" + srq + "' and rq<='" + erq + "' group by djbh,rq,dwbh,beizhu order by rq,djbh");
            }
            else
            {
                ds = mssql.ExecuteDs("select djbh,rq,dwbh,rtrim(beizhu) as beizhu,count(*) as tm from [spls_ck](nolock) where dwbh in(select dwbh from [mchk](nolock) where dwmch='" + dwmch + "') and rq>='" + srq + "' and rq<='" + erq + "' group by djbh,rq,dwbh,beizhu order by rq,djbh");
            }             
             
            if (ds != null && ds.Tables.Count > 0)
            {
                xd = new XmlDataDocument(ds);
                XmlNode root1 = xd.DocumentElement;

                XmlNodeList roots = root1.SelectNodes("ds");
                foreach (XmlNode item in roots)
                {
                    XmlNodeList list = item.SelectNodes("RecordDate");
                    ds.EnforceConstraints = false;  //如果需要修改xml里的数据  需要加上这句
                    foreach (XmlNode node in list)
                    {
                        //这里是修改XML中 RecordDate的时间格式 原始格式是：  <RecordDate>2012-04-20T16:16:00+08:00</RecordDate>
                        node.InnerText = Convert.ToDateTime(node.InnerText.ToString()).ToString("yyyy-MM-dd HH:mm");
                    }
                }
                return xd;
            }
            else
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    [WebMethod]
    public void Datainspection(string dwmch, string srq, string erq)
    {
        try {
            Context.Response.ContentType = "text/plain";
            DataSet ds = new DataSet();
            ds = mssql.ExecuteDs("select top 10 spid,spbh,spmch,shpgg,dw,shengccj from spkfk(nolock) where beactive='是' ");
            string result = Init.dsToJson(ds);
            Context.Response.Write(result);
        }
        catch (Exception ex)
        {
            Init.WriteLog(ex.ToString(), true);
            return;
        }
    }
}
